//
//  WPNewFirstCollectionViewCell.m
//  wobingwoyi
//
//  Created by WGP on 16/11/8.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "WPNewFirstTableViewController.h"
#import "WPFirstTableHeaderView.h"
#import "WPFirstTableFooterView.h"
#import "WPCredentialsTableViewCell.h"
#import "WPMinePlaceholderTextView.h"
#import "WPCredentialsEditeVC.h"
#import "WPFirstCredentialsEditeVC.h"
#import "ZNBPanNavigationController.h"
#import "WPMRModel.h"
#import "WPReturnListModel.h"
#import "WPAddNewReturnCaseVC.h"
#import "UITableView+FDTemplateLayoutCell.h"
#import "WPReturnCredentialsTableViewCell.h"

#define MARGIN 20.0
@interface WPNewFirstTableViewController ()<WPCredentialsTableViewCellDelegate,UIViewControllerTransitioningDelegate>

@property(nonatomic,strong)WPCredentialsTableViewCell *currentCell;
@property(nonatomic,strong)NSMutableArray *photos;
@property(nonatomic,strong)NSMutableArray *locations;

@property(nonatomic,strong)NSMutableArray *imagesArr;
@property(nonatomic,strong)WPFirstTableFooterView *footerView;

@end
@implementation WPNewFirstTableViewController

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
//    [[IQKeyboardManager sharedManager] setEnable:NO];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:[NSString stringWithUTF8String:object_getClassName(self)]];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
//    [[IQKeyboardManager sharedManager] setEnable:YES];
    [MobClick endLogPageView:[NSString stringWithUTF8String:object_getClassName(self)]];
}

-(void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setupTableView];
}

-(void)setupTableView
{
    self.tableView.backgroundColor = GlobalBgColor;
//    self.tableView.estimatedRowHeight = 10;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
    WPFirstTableFooterView *footView = [[WPFirstTableFooterView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
    
    footView.backgroundColor = GlobalBgColor;
    self.footerView = footView;
    
    __weak typeof(self) weakSelf = self;
    footView.addDiseaseBlick = ^{
    
        if (!(self.model.name.length && self.model.doctor.length && self.model.hospital && self.model.cureStatus.length == 3))
        {
            [self showInfoStatus:@"请把基本信息补充完整并保存"];
            return ;
        }
        
        WPAddNewReturnCaseVC *vc = [WPAddNewReturnCaseVC new];
        vc.mrModel = self.model;
        
        if(self.model.returnVisit_list.count)
        {
            WPReturnListModel *lastModel = [self.model.returnVisit_list lastObject];
            vc.newRank = lastModel.rank + 1;
        }
        else
        {
            vc.newRank = 2;
        }
        
        vc.reloadBlock = ^{
            
            [weakSelf.tableView reloadData];
            self.footerView.index = self.model.returnVisit_list.count + 2;
        };
        
        ZNBPanNavigationController *nav = [[ZNBPanNavigationController alloc] initWithRootViewController:vc];
        [weakSelf presentViewController:nav animated:YES completion:nil];
    };
    
    self.tableView.tableFooterView = footView;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.bounces = NO;
    
    WPFirstTableHeaderView *headerView = [WPFirstTableHeaderView new];
    self.headerView = headerView;
    
    weakSelf.baseInfoEditeBlock =headerView.baseInfoEditeBlock;
    
    self.tableView.tableHeaderView = headerView;
    [self.tableView registerClass:[WPCredentialsTableViewCell class] forCellReuseIdentifier:@"WPCredentialsTableViewCell"];
    [self.tableView registerClass:[WPReturnCredentialsTableViewCell class] forCellReuseIdentifier:@"WPReturnCredentialsTableViewCell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

-(void)setModel:(WPMRModel *)model
{
    _model = model;
    self.headerView.model = model;
    
    self.footerView.index = model.returnVisit_list.count + 2;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.model.returnVisit_list.count + 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.item == 0)
    {
           WPCredentialsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WPCredentialsTableViewCell"];
            cell.index = indexPath.item;
            cell.model = self.model;
            cell.delegate = self;
            return cell;
    }
    else
    {
            WPReturnCredentialsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WPReturnCredentialsTableViewCell"];
            cell.index = indexPath.item;
            cell.model = self.model;
            cell.delegate = self;
            return cell;
    }
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat itemWidth = (SCREEN_WIDTH - 4*10)/3.0;
    CGFloat itemHeight = itemWidth*90/112.5;
    if (indexPath.item == 0)
    {
            NSString *descpStr = self.model.firstOptionStr;
            CGSize size = [descpStr sizeWithMaxSize:CGSizeMake(SCREEN_WIDTH-20, MAXFLOAT) font:15];
            return size.height + itemHeight + 94;
            
    }
    else
    {
         UIImage *ZDImage = [UIImage imageNamed:@"zhenduan"];
         WPReturnListModel *model = self.model.returnVisit_list[indexPath.item-1];
            CGFloat itemWidth = (SCREEN_WIDTH - 4*10)/3.0;
            CGFloat itemHeight = itemWidth*90/112.5;
            
            NSString *descpStr = model.descpStr;
            CGSize size = [descpStr sizeWithMaxSize:CGSizeMake(SCREEN_WIDTH-20, MAXFLOAT) font:15];
            return itemHeight + 94 + size.height + 176 + ZDImage.size.height+30;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self WPCredentialsTableViewCellAddPicture:nil index:indexPath.item];
}

#pragma mark - 添加图片的代理方法
-(void)WPCredentialsTableViewCellAddPicture:(WPCredentialsTableViewCell *)cell index:(NSInteger)index
{
    self.currentCell = cell;
    
    if (index == 0)
    {
        WPFirstCredentialsEditeVC *vc = [WPFirstCredentialsEditeVC new];
        vc.model = self.model;
        vc.reloadBlock = ^(){
            
            [self.tableView reloadData];
        };
        
        ZNBPanNavigationController *nav = [[ZNBPanNavigationController alloc] initWithRootViewController:vc];
        [self presentViewController:nav animated:YES completion:nil];
    }
    else
    {
        WPCredentialsEditeVC *vc = [WPCredentialsEditeVC new];
        vc.model = self.model.returnVisit_list[index-1];
        vc.rank = index;
        ZNBPanNavigationController *nav = [[ZNBPanNavigationController alloc] initWithRootViewController:vc];
        
        __weak typeof(self) weakSelf = self;
        vc.reloadBlock = ^(){
           
            [weakSelf.tableView reloadData];
        };
        [self presentViewController:nav animated:YES completion:nil];
    }
    
}

-(NSMutableArray *)imagesArr
{
    if (_imagesArr == nil)
    {
        _imagesArr = [NSMutableArray array];
    }
    return _imagesArr;
}

@end
